//
//  ItemEditViewController.m
//  SAYAKA_Table
//
//  Created by numanuma08 on 2013/07/21.
//  Copyright (c) 2013年 勘米良. All rights reserved.
//

#import "ItemAddViewController.h"
#import "AppDelegate.h" 
#import "Item.h"
#import "Category.h"
#import "ItemImage.h"
#import "NSString+IsEmpty.h"

#define kSEGUE_SELECT_CATEGORY1 @"select_category1"
#define kSEGUE_SELECT_CATEGORY2 @"select_category2"
#define kSEGUE_SELECT_CATEGORY3 @"select_category3"

@interface ItemAddViewController ()
@property (strong, nonatomic) IBOutlet UITableViewCell *categoryCell1;
@property (strong, nonatomic) IBOutlet UITableViewCell *categoryCell2;
@property (strong, nonatomic) IBOutlet UITableViewCell *categoryCell3;
@property (strong, nonatomic) IBOutlet UITextField *nameTextField;
@property (strong, nonatomic) Category* category1;
@property (strong, nonatomic) Category* category2;
@property (strong, nonatomic) Category* category3;
@property (strong, nonatomic) ItemImage* itemImage;
- (void) onCancelAddItem;
@end

@implementation ItemAddViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _categoryCell1.detailTextLabel.text = _category1.name;
    _categoryCell2.detailTextLabel.text = _category2.name;
    _categoryCell3.detailTextLabel.text = _category3.name;
}

- (void) onCancelAddItem
{
    [self dismissViewControllerAnimated:YES completion:^{
        AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        NSManagedObjectContext* dbManager = appDelegate.managedObjectContext;
        [dbManager deleteObject:self.item];
        [appDelegate saveContext];
        self.cancelCallback(self);
    }];
}

- (IBAction)selectCancel:(id)sender
{
    [self onCancelAddItem];
}

- (IBAction)selectComplete:(id)sender
{
    if([_nameTextField.text isEmpty]){
        [self onCancelAddItem];
        return;
    }
    if(_category1 == nil){
        [self onCancelAddItem];
        return;
    }
    _item.name = _nameTextField.text;
    _item.category1 = _category1;
    _item.category2 = _category2;
    _item.category3 = _category3;
    _item.image = _itemImage;
    [self dismissViewControllerAnimated:YES completion:^{
        AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [appDelegate saveContext];
        self.completeCallback(self, _item);
    }];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:kSEGUE_SELECT_CATEGORY1] || [segue.identifier isEqualToString:kSEGUE_SELECT_CATEGORY2] || [segue.identifier isEqualToString:kSEGUE_SELECT_CATEGORY3]) {
        CategoryListViewController* destination = (CategoryListViewController*)segue.destinationViewController;
        destination.identifier = segue.identifier;
        destination.parent = self;
    }
}

# pragma mark Category list view delegate
-(void)listViewController:(CategoryListViewController *)listViewController didSelectedCategory:(Category *)category
{
    NSString* identifier = listViewController.identifier;
    if ([identifier isEqualToString:kSEGUE_SELECT_CATEGORY1]) {
        _category1 = category;
    } else if([identifier isEqualToString:kSEGUE_SELECT_CATEGORY2]){
        _category2 = category;
    } else {
        _category3 = category;
    }
    _categoryCell1.detailTextLabel.text = _category1.name;
    _categoryCell2.detailTextLabel.text = _category2.name;
    _categoryCell3.detailTextLabel.text = _category3.name;

}
@end
